package cn.smbms.controller;

import cn.smbms.pojo.User;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

//控制器：IndexController
@Controller
public class IndexController{
    private Logger logger = Logger.getLogger(IndexController.class);

//    @RequestMapping表示方法与哪个请求URL来对应（此处："/index"）
    @RequestMapping("/index")
    public String index(){
        logger.debug("学框架就学Spring MVC!");
        return "index";
    }

    @RequestMapping("/test")
    public String test(@RequestParam String userCode,Model model){
        System.out.println("welcome! userCode：" + userCode);
        model.addAttribute("userCode",userCode);
        return "success";
    }

    /**
     * 参数入参：View to Controller
     */
//    @RequestMapping(value = "/welcome",method = RequestMethod.GET,params = "username")
    @RequestMapping("/welcome")
    public String welcome(@RequestParam(value = "username",required = false) String username){
        logger.info("welcome,"+username);
        return "index";
    }

    /**
     * 参数传递：Controller to View -(ModelAndView)
     */
    @RequestMapping("/index1")
    public ModelAndView index(String username){
        logger.info("welcome! username："+username);
        ModelAndView mView = new ModelAndView();
        mView.addObject("username",username);
        mView.setViewName("index");
        return mView;
    }

    /**
     * 参数传递：Controller to View -(Model)
     */
    @RequestMapping("/index2")
    public String index(String username, Model model){
        logger.info("hello,SpringMVC! username：" + username);
        model.addAttribute("username",username);
        model.addAttribute(username);
        User user = new User();
        user.setUserName(username);
        model.addAttribute("currentUser",user);
        model.addAttribute(user);
        return "index";
    }

    /**
     * 参数传递：Controller to View -(Map<String,Object>model)
     */
    @RequestMapping("/index3")
    public String index(String username, Map<String,Object> model){
        logger.info("hello,SpringMVC! username：" + username);
        model.put("username",username);
        return "index";
    }

    @RequestMapping("/add")
    public String a(){
        return "add";
    }
}
